package cs276.pe1.lucene;

import java.io.File;
import java.util.logging.Logger;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

import cs276.pe1.lucene.IMDBParser.MoviePlotRecord;

/**
 * Indexes the IMDB data set.  This should only be run once.
 * @author alecmgo@gmail.com
 */
public class IMDBIndexer {
  private static Logger logger = Logger.getLogger("cs276.pe1.lucene.IMDBIndexer");

  public static void main(String[] argv) throws Exception {
    File indexPath = new File(new File(System.getProperty("user.home")),"cs276-index");
    indexPath.mkdir();

    IndexWriter writer = new IndexWriter(indexPath, new StandardAnalyzer(), true);

    for (MoviePlotRecord rec : IMDBParser.readRecords()) {
      Document doc = new Document();

      doc.add(new Field("title", rec.title, Field.Store.YES, Field.Index.TOKENIZED));
      doc.add(new Field("plots", rec.plots, Field.Store.YES, Field.Index.TOKENIZED));
      doc.add(new Field("authors", rec.authors, Field.Store.YES, Field.Index.TOKENIZED));

      writer.addDocument(doc);
      logger.finest(rec.title + " " + rec.plots);
    }
    
    writer.close();
    System.err.println("Done Indexing");
  }
}
